package com.haungg.controller.account;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import json.JSONResult;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import tree.TreeNode;
import tree.TreeObject;
import base.BaseController;

import com.haungg.po.TMenu;
import com.haungg.po.TUser;
import com.haungg.user.UserService;

/**  
 * Filename:    LoginController.java
 * Description: 登录控制层，以springMVC注解的方式实现
 * Company:     
 * @author:     Jiang.hu
 * @version:    1.0
 * Create at:   May 6, 2011 2:14:20 PM
 * modified:    
 */
@Controller
public class LoginController extends BaseController{
	@Resource
	UserService userService;
	/* 
	 * @RequestMapping 方法的返回类型
	 * 		ModelAndView 
	 * 		Model
	 * 		A Map object for exposing a model
	 * 		View 
	 * 		String 指定view name
	 * 		void if the method handles the response itself
	 * 		If the method is annotated with @ResponseBody,the return type is written to the response HTTP body
	 * redirect, forward跳转
	 * 		使用"redirect:"前缀,如 "redirect:/view" 
	 * 		相应地使用"forward:"前缀表示servlet内部跳转
	 * Handling a file upload in a form
	 * 		使用@RequestParam("file") MultipartFile file获取上传的文件  
	 */      
	@RequestMapping(value="/login.do",method={RequestMethod.GET,RequestMethod.POST})
	public String login(Model model,TUser user){
		
		boolean isLegal = userService.findUser(user);
		//if(isLegal){
			List<TMenu> list = userService.getTopMenu();
			model.addAttribute("menuList", list);
			return "login/main";
		//}
		//return "login";
	}
	
	public void writeJson(HttpServletResponse response, JSONResult jr) {
		response.setContentType("text/x-json;charset=utf-8");
		response.setHeader("Cache-Control", "no-store, max-age=0, no-cache, must-revalidate");
		response.addHeader("Cache-Control", "post-check=0, pre-check=0");
		response.setHeader("Pragma", "no-cache");
		try {
			PrintWriter out = response.getWriter();
			out.write(jr.toString());
			out.close();
		} catch (IOException e) {
			//logger.error("error write json data", str);
		}
	}
	
	
/*	@RequestMapping(value="/login.do",method={RequestMethod.GET,RequestMethod.POST})
	public ModelAndView login(HttpServletRequest request,HttpServletResponse response){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
		String email = request.getParameter("email");
		String passWord = request.getParameter("passWord");
		String nowTime = sdf.format(new Date());
		
		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("email", email);
		modelMap.put("passWord", passWord);
		modelMap.put("nowTime", nowTime);
		
		ModelMap modelMap2 = new ModelMap();
		modelMap2.addAttribute("email", email);
		modelMap2.addAttribute("passWord",passWord);
		modelMap2.addAttribute("nowTime", nowTime);
		
		
		 * 返回ModelAndView对象，常见的几种方式
		 * view-返回网页名称，在springMVC文件中已经配置了前缀和后缀
		 * model数据可以直接用键值对，也可用hashmap、modelmap以及addobject的方式
		 * 
//		return new ModelAndView("index","nowTime",nowTime);
//		return new ModelAndView("index",modelMap);
//		return new ModelAndView("index",modelMap2);
		return new ModelAndView("index").addObject("email", email).addObject("passWord", passWord).addObject("nowTime", nowTime);
	}
*/
	@RequestMapping(value="/left.do",method={RequestMethod.GET,RequestMethod.POST})
	public String left(Model model, Integer sid){
		TreeObject<Integer, TMenu> tree = userService.getChildMenu(sid);
		tree.setNode(new TreeNode<Integer, TMenu>(0, -1, new TMenu()));
		model.addAttribute("tree", tree);
		return "login/left";
	}
	@RequestMapping(value="/top.do",method={RequestMethod.GET,RequestMethod.POST})
	public String top(HttpServletRequest request,HttpServletResponse response){
		
		return "login/top";
	}
	@RequestMapping(value="/main.do",method={RequestMethod.GET,RequestMethod.POST})
	public String main(Model model, Integer sid){
		List<TMenu> list = userService.getTopMenu();
		model.addAttribute("menuList", list);
		return "main";
	}

@RequestMapping(value="/save.do",method={RequestMethod.GET,RequestMethod.POST})
public String save(HttpServletRequest request,HttpServletResponse response){
	List<TUser> list = new ArrayList<TUser>();
	TUser user = new TUser();
	user.setId(2);
	user.setLoginid("A0011");
	user.setUsername("test");
	user.setPassword("123");
	//userService.save(user);
	TUser user1 = new TUser();
	user1.setId(3);
	user1.setLoginid("A0012");
	user1.setUsername("test");
	user1.setPassword("123");
	//userService.save(user1);
	list.add(user);
	list.add(user1);
	userService.aasaveList(list);
	return "index";
}

}
